package com.longsunhd.yum.buffalo.activity;

import android.graphics.drawable.BitmapDrawable;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.PopupWindow;

import com.longsunhd.yum.buffalo.R;
import com.longsunhd.yum.buffalo.adapter.ArrayAdapter;
import com.longsunhd.yum.buffalo.base.BaseActivity;
import com.longsunhd.yum.buffalo.base.BaseApplication;
import com.longsunhd.yum.buffalo.bean.LoginBean;
import com.longsunhd.yum.buffalo.constant.Constant;
import com.longsunhd.yum.buffalo.network.action.UserApiUtils;
import com.longsunhd.yum.buffalo.util.ActivitySkipUtil;
import com.longsunhd.yum.buffalo.util.StatusBarUtil;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;
import cc.fussen.cache.Cache;

/**
 * 作者：黄宣淇
 * 时间：2021/5/7 0007
 * 如何代码没有备注，一个月后，只有你和上帝知道--鲁迅
 */
public class LoginActivity extends BaseActivity implements ArrayAdapter.DeleteI {

    @BindView(R.id.et_mobile)
    protected EditText et_mobile;
    @BindView(R.id.iv_moreAcount)
    protected View iv_moreAcount;
    @BindView(R.id.et_password)
    protected EditText et_password;
    @Override
    protected int getContentView() {
        return R.layout.activity_login;
    }
    @Override
    protected void initData() {
        super.initData();
    }

    @Override
    protected void initWidget() {
        super.initWidget();
        //设置状态栏字体颜色
        StatusBarUtil.setLightMode(this);
    }

    @OnClick(R.id.btn_login)
    public void btn_login() {
        String mobile = "", password = "";
        mobile = et_mobile.getText().toString();
        password = et_password.getText().toString();
        if (TextUtils.isEmpty(mobile)) {
            BaseApplication.showToast("请输入手机号");
            return;
        }
        if (TextUtils.isEmpty(password)) {
            BaseApplication.showToast("请输入密码");
            return;
        }
        login(mobile, password);
    }

    private void login(String username, String password) {
        try {
            Map<String,String> map=new HashMap<>();
            map.put("username",username);
            map.put("password",password);
            UserApiUtils.newInstance().loginMap(map, new Handler() {
                @Override
                public void handleMessage(Message msg) {
                    super.handleMessage(msg);
                    LoginBean loginBean = (LoginBean) msg.obj;
                    if (loginBean.getCode() == Constant.Success) {
                        /*保存信息*/
                        /*保存登录的列表*/
                        LinkedList<LoginBean> loginBeanLinkedList =
                                Cache.with(LoginActivity.this).getCache(Constant.UserInfoBeanList, LinkedList.class);
                        loginBean.getData().setName(et_mobile.getText().toString());
                        loginBean.getData().setPassword(et_password.getText().toString());
                        if (loginBeanLinkedList != null) {
                            boolean isHash = false;
                            int index = -1;
                            for (int i = 0; i < loginBeanLinkedList.size(); i++) {
                                LoginBean bean = loginBeanLinkedList.get(i);
                                if (bean.getData().getName().equals(loginBean.getData().getName())) {
                                    isHash = true;
                                    index = i;
                                    break;
                                }
                            }
                            if (isHash && index != -1) {
                                loginBeanLinkedList.remove(index);
                            }
                        } else {
                            loginBeanLinkedList = new LinkedList<>();
                        }
                        loginBeanLinkedList.addFirst(loginBean);
                        Cache.with(LoginActivity.this).saveCache(Constant.UserInfoBeanList, loginBeanLinkedList);

                        /*跳转*/
                        HashMap<String, LoginBean> hashMap = new HashMap<>();
                        hashMap.put(Constant.loginBean, loginBean);
                        ActivitySkipUtil.skipAnotherActivity(LoginActivity.this, MainActivity.class, hashMap,true);

                    } else {
                        BaseApplication.showToast(loginBean.getMsg() + "");
                    }
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    private PopupWindow pop;//更多信息
    ListView listView;
    LinkedList<LoginBean> userLoginBeanArrayList;//临时保存用户名的

    @OnClick(R.id.iv_moreAcount)
    public void iv_moreAcount() {
        try {
            /*初始化列表*/
            userLoginBeanArrayList =
                    Cache.with(LoginActivity.this).getCache(Constant.UserInfoBeanList, LinkedList.class);
            if (userLoginBeanArrayList == null || userLoginBeanArrayList.size() == 0) {
                BaseApplication.showToast("没有账号信息");
                return;
            }
            View view = View.inflate(this, R.layout.layout_accout_listview, null);
            if (pop == null) {
                pop = new PopupWindow(view, et_mobile.getWidth(), AbsListView.LayoutParams.WRAP_CONTENT, true);
            }
            if (userLoginBeanArrayList.size() > 0) {
                final ArrayAdapter adapter = new ArrayAdapter(this, userLoginBeanArrayList);
                adapter.setDeleteI(this);
                listView = (ListView) view.findViewById(R.id.listView);
                listView.setAdapter(adapter);
                listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        et_mobile.setText(userLoginBeanArrayList.get(position).getData().getName());
                        et_password.setText(userLoginBeanArrayList.get(position).getData().getPassword());
                        adapter.notifyDataSetChanged();
                        pop.dismiss();
                    }
                });
            }
            pop.setBackgroundDrawable(new BitmapDrawable());
            pop.setFocusable(true);
            pop.setOutsideTouchable(true);
            pop.showAsDropDown(et_mobile, 0, 0);
            pop.update();
        } catch (Exception e) {
            e.getMessage();
        }
    }

    @Override
    public void deleteClick(LoginBean userLoginBean) {
        userLoginBeanArrayList.clear();
        LinkedList<LoginBean> loginBeanLinkedList =
                Cache.with(LoginActivity.this).getCache(Constant.UserInfoBeanList, LinkedList.class);
        for (int i = 0; i < loginBeanLinkedList.size(); i++) {
            LoginBean bean = loginBeanLinkedList.get(i);
            if (bean.getData().getName().equals(userLoginBean.getData().getName())) {
                loginBeanLinkedList.remove(i);
                break;
            }
        }
        userLoginBeanArrayList = loginBeanLinkedList;
        Cache.with(LoginActivity.this).saveCache(Constant.UserInfoBeanList, userLoginBeanArrayList);
        ArrayAdapter adapter = new ArrayAdapter(this, userLoginBeanArrayList);
        listView.setAdapter(adapter);
        adapter.notifyDataSetChanged();
        pop.dismiss();
    }
}
